clear
est clear


cap mat define migrationfigure=J(105,10,.)
		local row=1

	use "$input/full_geoid_pair_panel.dta"
	
	
			
	replace migration=migration/(tot_pop/100000)

	//Make sure only include states that have a storm in at least one county at least once in our sample years
	bysort state_fips: egen max=max(storm)
		drop if max!=1
		
	//Create future pooled migration - total migrants in the subsequent five years
	sort geoid_pair move_year
	g post_storm=1 if storm==1|storm[_n-1]==1&geoid_pair==geoid_pair[_n-1]|storm[_n-2]==1&geoid_pair==geoid_pair[_n-2]|storm[_n-3]==1&geoid_pair==geoid_pair[_n-3]|storm[_n-4]==1&geoid_pair==geoid_pair[_n-4]|storm[_n-5]==1&geoid_pair==geoid_pair[_n-5]
	mvencode post_storm, mv(0)
	
	//Collapse to total migration from a county (not looking at paired county to county migration, just all migration out of a county)
	collapse (sum) migration (mean) share_approved totalapprovedihpamount,by(from_geoid move_year storm post_storm state_fips)
	
	//Create IHS transformations
	ihstrans(migration)
	
	
	//Create lags of storms (was there a storm in t-1 years)

			
	label var post_storm "1(0-5 yrs post storm)"
	label var storm "1(Storm year)"
	sort from_geoid move_year
	
	replace totalap=totalap/1000000
	g highdamage=(totalapp>10 & totalapp<20) if totalapp!=.
		replace highdamage=. if totalapp>20
	mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
	foreach var in L1 L2 L3 L4 L5{
		label var highdamage_`var' "1(Storm year), `var'"
	}
		
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
		
			eststo t1c1
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]=15
			local row=`row'+1
		
		drop highdamage*
		g highdamage=(totalapp>20 & totalapp<40) if totalapp!=.
			replace highdamage=. if totalapp>40	
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
	
			eststo t1c2
			cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]=30
			local row=`row'+1
		
		drop highdamage*
		g highdamage=(totalapp>40 & totalapp<80) if totalapp!=.
			replace highdamage=. if totalapp>800
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			

		eststo t1c3
		cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]=60
			local row=`row'+1		
		
		drop highdamage*
		g highdamage=(totalapp>80 & totalapp<160) if totalapp!=.
			replace highdamage=. if totalapp>160
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)

		eststo t1c4
		cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]=120
			local row=`row'+1	
		
		
		drop highdamage*
		g highdamage=(totalapp>160) if totalapp!=.
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)

		eststo t1c5
		cap mat def migrationfigure[`row',1]=_b[1.highdamage]
			cap mat def migrationfigure[`row',2]=_se[1.highdamage]
			cap mat def migrationfigure[`row',3]=200
			local row=`row'+1
			

label var highdamage "1(Storm year)"				
				
	file open 	t 		using "$tables/table_si17.tex", replace write
	file write t	"\begin{table}[htbp]\footnotesize \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
						"\caption{Migration impacts of high damage storms, bins}\label{tab: total migration effects high damage bins}" _n  ///
						"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{10}{c}}" _n "\midrule" _n ///
						"&$10MM$-$20MM$&$20MM$-$40MM$&$40MM$-$80MM$&$80MM$-$160MM$&$>160MM$&\\" _n ///
						"\midrule" _n 
	file close 	t
	
				esttab t1c* using "$tables/table_si17.tex", l keep(1.highdamage) ///
				s(N, l("N") f(%11.0fc) lay(@)) $opts 
				
	file open 	t 		using "$tables/table_si17.tex", append write
	file write t "\\" ///
							"\midrule																			" ///
							"Fixed Effects:									&		&		&		&		&					\\" ///
							"\hspace{3mm}County								&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
							"\hspace{3mm}Year								&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
							"\hspace{3mm}State\$\times\$Year				&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
							"\midrule" _n "\end{tabular*}" _n ///
							"\begin{tabular*}{1\textwidth}{p{6in}}" _n ///
							"\footnotesize \textsc{Notes:} All columns report the results of a fixed effects specification with the IHS transformation of the number of out-migrants from a county as the outcome. Counties are defined as exposed to a storm if at least one hurricane resulted in a flood warning during the year or the county experienced wind speeds of at least 21 m/s - the speed at which structural damage begins to occur - during a hurricane in the year and the total value of compensation approved by FEMA was greater than indicated in the column heading. Roughly 90\% of storms in our sample result in less than \$10 million in approved compensation. 95\% result in less than \$20 million in approved compensation. \$^{*}\$p=0.1, \$^{**}\$p=0.05,\$^{***}\$p=0.01." ///
							"\end{tabular*}" _n "\end{table}" _n 
	file close 	t		
	
	
	
	
	
			